package com.modular.extra.fm.data.push.opt;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.c.opt.ComOpt;
import com.modular.extra.fm.FactoryFmOpt;
import com.modular.extra.fm.data.push.bean.FmDataPush;

public class OptFmDataPush extends ComOpt<FmDataPush> {

    public OptFmDataPush() {
        super("data_push_record", FactoryFmOpt.DS_ID);
    }

    /**
     * 生成并保存信息
     * @param mn
     * @param dataItemId
     * @param content
     */
    public boolean genAndSaveMsg(String mn, String dataItemId, String content) {
        return genData(mn, dataItemId, content, 1);
    }

    /**
     * 生成事件异常信息
     * @param mn
     * @param dataItemId
     * @param content
     * @param eventType
     * @return
     */
    public boolean genData(String mn, String dataItemId, String content, Integer eventType) {
        // 查询历史数据
        FmDataPush fmDataPush = query().eq("data_item_id", dataItemId).getBean();
        if (ObjectUtil.isNotNull(fmDataPush)) {
            return false;
        }
        fmDataPush = new FmDataPush();
        fmDataPush.setDataItemId(dataItemId);
        fmDataPush.setPushType("0");
        fmDataPush.setPushState("0");
        fmDataPush.setContent(content);
        fmDataPush.setMn(mn);
        fmDataPush.setCreatedAt(DateUtil.now());
        fmDataPush.setEventType(eventType);
        syncBean(fmDataPush);
        return true;
    }
}
